package xyz.kail.demo.easyrules.hello.stream;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;

/**
 * https://zhuanlan.zhihu.com/p/91158525
 */
public class StreamRuleMain {
    public static void main(String[] args) {

        Rule rule = new RuleBuilder()
                .name("weather rule")
                .description("if it rains then take an umbrella")
                /* 条件 */
                .when(facts -> facts.get("rain").equals(true))
                /* 执行 */
                .then(facts -> System.out.println("It rains, take an umbrella!"))
                .build();

        // 注册规则
        Rules rules = new Rules();
        rules.register(rule);

        // 规则引擎
        RulesEngine rulesEngine = new DefaultRulesEngine();

        // 参数条件
        Facts facts = new Facts();
        facts.put("rain", true);

        // 触发规则
        rulesEngine.fire(rules, facts);

    }
}